/**
 * 
 */
package x.java.tea;

import java.io.ByteArrayInputStream;
import java.util.Map;

/**
 * @author qiangli
 *
 */
public class TemplateObjectInjector extends ObjectInjector {
	protected String template;

	/**
	 * 
	 */
	public TemplateObjectInjector(String template) {
		this.template = template;
	}
	
	public TemplateObjectInjector(Object obj, String template) {
		super(obj);
		this.template = template;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void setBuf(Object obj) {
		Injector inj = null;
		if (obj instanceof Injector) {
			inj = (Injector)obj;
		} else if (obj instanceof Object[]) {
			inj = new ListInjector((Object[])obj);
		} else if (obj instanceof Map) {
			inj = new MapInjector((Map)obj);
		} else {
			inj = new ObjectPropertyInjector(obj);
		}
		ByteArrayInputStream is = new ByteArrayInputStream(template.getBytes());
		FilterInputStream fis = new FilterInputStream(is,inj);
		setBuf(new InputStreamInjector(fis));
	}
}
